/**
 * Created by user-007 on 2018/3/7.
 */
import { message } from 'antd';
import { routerRedux } from 'dva/router';
import { get${className}List, get${className}Detail, delete${className},
  add${className}, update${className} } from '../services/${classname}';

export default {
  namespace: '${classname}',

  state: {
    page: 1,
    pageSize: 10,
    total: 0,
    name: '',
    loading: false,
    dataSource: [],

    // 详情
    ${classname}Detail: null

  },


  effects: {
  *fetchList({ payload }, { put, call }) {
  const res = yield call(get${className}List, payload);
  if (res.success) {
    yield put({
      type: 'changeFetchStatus',
      payload: true,
    });
    res.data.recordset.map((item) => {
      item.key = item.id;
    return item;
  });
    res.data.name = payload.name;
    yield put({
      type: 'updateFetchParam',
      payload: res.data,
    });
    yield put({
      type: 'changeFetchStatus',
      payload: false,
    });
  } else {
    message.error(res.message);
  }
},
  *delete({ payload }, { put, call }) {
  const { id } = payload;
  const res = yield call(delete${className}, id);
  if (res.success) {
    message.success('删除成功');
    const response = yield call(get${className}List, payload);
    if (response.success) {
      yield put({
        type: 'changeFetchStatus',
        payload: true,
      });
      response.data.recordset.map((item) => {
        item.key = item.id;
      return item;
    });
      response.data.name = '';
      yield put({
        type: 'updateFetchParam',
        payload: response.data,
      });
      yield put({
        type: 'changeFetchStatus',
        payload: false,
      });
    } else {
      message.error(response.message);
    }
  } else {
    message.error(res.message);
  }
},
  *detail({ payload }, { put, call }) {
  const res = yield call(get${className}Detail, payload);
  if (res.success) {
    yield put({
      type: 'updateDetail',
      payload: {
        data: res.data
      }
    });
  } else {
    message.error(res.message);
  }
},
  *add({ payload }, { put, call }) {
  const res = yield call(add${className}, payload);
  if (res.success) {
    message.success('添加成功!');
    yield put(routerRedux.push('/home/${classname}/list'));
  } else {
    message.error(res.message);
  }
},
  *update({ payload }, { put, call }) {
  const res = yield call(update${className}, payload);
  if (res.success) {
    message.success('编辑成功');
    setTimeout(1000, yield put(routerRedux.push('/home/${classname}/list')));
  } else {
    message.error(res.message);
  }
}
},

reducers: {
  changeFetchStatus(state, { payload }) {
    return {
        ...state,
        loading: payload,
  };
  },
  updateFetchParam(state, { payload }) {
    // console.log(payload, 'update');
    return {
        ...state,
        page: payload.paging.page,
        pageSize: payload.paging.pageSize,
        total: payload.paging.records,
        dataSource: payload.recordset,
        name: payload.name || '',
  };
  },
  updateDetail(state, { payload }) {
    return {
        ...state,
        ${classname}Detail: payload.data,
  };
  }
},
};
